package com.mapper;

import com.pojo.Follows;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FollowsMapper {

    @Insert("INSERT INTO follows(follower_id, followed_id, created_at) " +
            "VALUES(#{followerId}, #{followedId}, #{createdAt})")
    int insert(Follows follow);

    @Delete("DELETE FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int delete(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

    @Delete("DELETE FROM follows WHERE follower_id = #{followerId}")
    int deleteByFollowerId(Integer followerId);

    @Delete("DELETE FROM follows WHERE followed_id = #{followedId}")
    int deleteByFollowedId(Integer followedId);

    @Select("SELECT * FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    Follows selectByFollowerAndFollowed(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

    @Select("SELECT * FROM follows WHERE follower_id = #{followerId}")
    List<Follows> selectByFollowerId(Integer followerId);

    @Select("SELECT * FROM follows WHERE followed_id = #{followedId}")
    List<Follows> selectByFollowedId(Integer followedId);

    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{followerId}")
    int countFollowing(Integer followerId);

    @Select("SELECT COUNT(*) FROM follows WHERE followed_id = #{followedId}")
    int countFollowers(Integer followedId);

    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int exists(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);
}